package com.ruoyi.supermarket.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.supermarket.bean.ProductDetail;
import com.ruoyi.supermarket.domain.Product;
import com.ruoyi.supermarket.service.ProductService;
import com.ruoyi.supermarket.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品Controller
 * 
 * @author 李其伟
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/supermarket/product")
public class ProductController extends BaseController
{
    @Autowired
    private ProductService productService;

    @Autowired
    private ShopService shopService;


    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        //店铺用户只能查询自己的数据
        if (hasShop()) {
            product.setShopId(shopService.selectShopByUserId(getUserId()));
        }
        startPage();
        List<Product> list = productService.selectProductList(product);
        TableDataInfo tableDataInfo = getDataTable(list);

        List<Product> productList = (List<Product>) tableDataInfo.getRows();
        String[] skus = productList.stream().map(Product::getSku).toArray(String[]::new);
        List<ProductDetail> pdList = productService.selectProductDetailBySkus(skus);

        tableDataInfo.setRows(pdList);
        return tableDataInfo;
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }

    /**
     * 查询商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:query')")
    @GetMapping(value = "/query/{key}")
    public AjaxResult queryProductInfo(@PathVariable("key") String key)
    {
        return success(productService.queryProductByKey(key));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductDetail pd)
    {
        Product product = pd.getProduct();
        product.setShopId(shopService.selectShopByUserId(getUserId()));
        pd.setProduct(product);
        return toAjax(productService.insertProduct(pd));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductDetail pd)
    {
        Product product = pd.getProduct();
        product.setShopId(shopService.selectShopByUserId(getUserId()));
        pd.setProduct(product);
        return toAjax(productService.updateProduct(pd));
    }

    /**
     * 提交商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:commit')")
    @Log(title = "提交商品", businessType = BusinessType.GRANT)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody Product req)
    {
        return toAjax(productService.commitProduct(req));
    }

    /**
     * 审核商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:audit')")
    @Log(title = "审核商品", businessType = BusinessType.GRANT)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody Product req)
    {
        return toAjax(productService.auditProduct(req));
    }

    /**
     * 上下架商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:publish')")
    @Log(title = "上下架商品", businessType = BusinessType.GRANT)
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody Product req)
    {
        return toAjax(productService.publishProduct(req));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('supermarket:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
